package com.graduate.game.assignment.controller;

import com.alibaba.fastjson.JSON;
import com.graduate.game.assignment.pojo.assignment;
import com.graduate.game.assignment.pojo.record;
import com.graduate.game.assignment.service.recordService;
import com.graduate.game.user.pojo.userdata;
import com.graduate.game.user.service.UserdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
@RequestMapping("/record")
@CrossOrigin(origins = "*", maxAge = 3600)
public class recordController {
    @Autowired
    public recordService recordservice;
    @RequestMapping("/produce")
    public String publish(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        record record = new record();
        System.out.println(s1);
        Map maps =(Map) JSON.parse(s1);
        Date date = new Date(System.currentTimeMillis()-5400000);
        record.setAssginid(Integer.parseInt(maps.get("assginid").toString()));
        record.setGetuser((String) maps.get("getuser"));
        record.setPublisher((String) maps.get("publisher"));
        record.setTitle((String) maps.get("title"));
        record.setTime(date);
        recordservice.insert(record);
        return "产生了记录";
    }
    //待完成任务
    @RequestMapping("/getBygetuser")
    public List<record> getBygetuser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        List<record> records = recordservice.getreRecordsByID(s1);
        ArrayList<record> objects = new ArrayList<>();
        for(int i = 0;i < records.size(); i ++){
            if(records.get(i).getState()==0||records.get(i).getState()==3)
            {
                objects.add(records.get(i));
            }
        }
        System.out.println("未完成："+objects);
        return objects;
    }
    //已完成任务
    @RequestMapping("/OKgetBygetuser")
    public List<record> OkgetBygetuser(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        List<record> records = recordservice.getreRecordsByID(s1);
        ArrayList<record> objects = new ArrayList<>();
        for(int i = 0;i <records.size(); i++){
            if(records.get(i).getState()==1)
            {
               objects.add(records.get(i));
            }
        }
        System.out.println("已完成："+objects);
        return objects;
    }

    //根据ID获取一条记录
    @RequestMapping("/byID")
    public record ByID(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        record record = recordservice.getoneRecord(Integer.parseInt(s1));
        return record;
    }

    //提交任务，等待发布者审核
    @RequestMapping("/shenhe")
    public String shenhe(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map)JSON.parse(s1);
        record record = new record();
        record.setId(Integer.parseInt(maps.get("id").toString()));
        record.setState(3);
        record.setAnnex((String) maps.get("annex"));
        record.setContent((String) maps.get("content"));
        System.out.println(record.toString());
        recordservice.updateRecord(record);
        return "提交成功";
    }

    //审核
    @RequestMapping("/shenheing")
    public List<record> shenheing(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        System.out.println(s1);
        System.out.println("进入审核");
        List<record> records = recordservice.getrecordsbypulishRecords(s1);
        ArrayList<record> objects = new ArrayList<>();
        for(int i = 0;i <records.size(); i++){
            if(records.get(i).getState()==3)
            {
                objects.add(records.get(i));
            }
        }
        System.out.println("筛选出的审核名单"+objects);
        return objects;
    }
    @Autowired
    public UserdataService userdataService;
    //审核决定
    @RequestMapping("/updateState")
    public String updateState(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setHeader("Access-Control-Allow-Origin","*");
        String s1 = request.getReader().readLine();
        Map maps =(Map)JSON.parse(s1);

        //获取任务所奖励的金币
        int gold = Integer.parseInt(maps.get("gold").toString());
        System.out.println("任务奖励金币："+gold);

        //获取发布者和接收者的资产信息
        userdata publisherdata = userdataService.getOneUserdata((String) maps.get("publisher"));
        userdata getuserdata = userdataService.getOneUserdata((String) maps.get("getuser"));

        //更新金币操作
           //减少金币publisher
        long publishergold = publisherdata.getGold();
        System.out.println("任务发布者拥有金币："+publishergold);
        publishergold=publishergold-gold;
        System.out.println("任务发布者剩余金币："+publishergold);
        userdata publisheruserdata = new userdata();
        publisheruserdata.setUsername((String) maps.get("publisher"));
        publisheruserdata.setGold(publishergold);
        userdataService.updateGoldRank(publisheruserdata);
            //增加金币getuser
        long getusergold = getuserdata.getGold();
        System.out.println("接受任务者的金币："+getusergold);
        getusergold=getusergold+gold;
        System.out.println("接收者布者剩余金币："+getusergold);
        userdata getuserdatagold = new userdata();
        getuserdatagold.setGold(getusergold);
        getuserdatagold.setUsername((String) maps.get("getuser"));
        userdataService.updateGoldRank(getuserdatagold);


        //更新经验
           //获取接收人者的原始校力值
        long experience = getuserdata.getExperience();
        System.out.println("接受任务者原来的经验值："+getusergold);
        experience=experience+10;
        userdata userdataEx = new userdata();
        userdataEx.setExperience(experience);
        userdataEx.setUsername((String) maps.get("getuser"));
        userdataService.updateEx(userdataEx);

        //更新state操作
        record record = new record();
        record.setState(1);
        record.setId(Integer.parseInt(maps.get("id").toString()));
        recordservice.updatestate(record);
        return "审核成功";
    }
}
